package gw.com.android.ui.gesture_pwd;


import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

import java.util.List;

import butterknife.BindView;
import gw.com.android.app.ActivityManager;
import gw.com.android.app.AppMain;
import gw.com.android.app.GTConfig;
import gw.com.android.presenter.PresenterImpl;
import gw.com.android.ui.gesture_pwd.base.GestureBaseActivity;
import gw.com.android.ui.gesture_pwd.custom.CommonDialog;
import gw.com.android.ui.gesture_pwd.custom.EasyGestureLockLayout;
import gw.com.android.ui.onlive.VipDialog;
import gw.com.android.utils.CacheUtils;
import www.com.library.dialog.ToastPopWindow;

/**
 * 手势密码 重新设置；
 * 首先校验原密码，在校验通过之后，重新设置手势；
 * 如果5次校验原密码都失败了，则弹窗，跳转到 登录界面；
 */
public class GesturePwdResetActivity extends GestureBaseActivity implements PresenterImpl {

    @BindView(R.id.layout_small)
    EasyGestureLockLayout layout_small;
    @BindView(R.id.tv_go)
    TextView tv_go;
    @BindView(R.id.tv_redraw)
    TextView tv_redraw;
    @BindView(R.id.layout_parent)
    EasyGestureLockLayout layout_parent;

    @Override
    public void onRequestSuc(Object result) {

    }

    @Override
    public void onRequestFail(int failCode) {

    }

    @Override
    public void onBackPressed() {
        //这里不接受back按键
        animate(tv_go);
    }

    @Override
    protected int getLayoutView() {
        return R.layout.layout_gesture_pwd_reset;
    }

    @Override
    protected void initLayoutView() {
        EasyGestureLockLayout.GestureEventCallbackAdapter adapter = new EasyGestureLockLayout.GestureEventCallbackAdapter() {

            @Override
            public void onSwipeFinish(List<Integer> pwd) {
                layout_small.refreshPwdKeyboard(pwd);//通知另一个小密码盘，将密码点展示出来，但是不展示轨迹线
                if (layout_parent.getCurrentMode() == EasyGestureLockLayout.STATUS_RESET)
                    tv_redraw.setVisibility(View.VISIBLE);
                else
                    tv_redraw.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onResetFinish(List<Integer> pwd) {
                super.onResetFinish(pwd);
                savePwd(showPwd("showGesturePwdInt", pwd));//保存密码到本地
                new ToastPopWindow(GesturePwdResetActivity.this, AppMain.getAppString(R.string.gesture_pwd_set_success)).show();//弹窗表示，设置成功
                GTConfig.instance().setBooleanValue(GTConfig.PRE_OPEN_GESTURE, true);//把 系统设置“是否启用手势密码”标志位设置为true
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        finish();//然后结束自己
                    }
                }, 500);

            }

            @Override
            public void onCheckFinish(boolean succeedOrFailed) {
                tv_go.setText("请输入新的手势密码");
                if (succeedOrFailed) {//如果解锁成功，则切换到set模式
                    layout_parent.switchToResetMode();
                } else {
                    showConfirmDialog();//弹窗，给出一个确定按钮，告诉用户，点击确定后，进入登录界面
                }
            }

            @Override
            public void onSwipeMore() {
                //执行动画
                animate(tv_go);
            }

            @Override
            public void onToast(String s, int textColor) {
                tv_go.setText(s);
                if (textColor != 0)
                    tv_go.setTextColor(textColor);
                if (textColor == 0xffFF3232) {
                    animate(tv_go);
                }
            }
        };
        layout_parent.setGestureFinishedCallback(adapter);

        if (!TextUtils.isEmpty(CacheUtils.readFile(CacheUtils.get_CACHE_GESTURE_PWD_INT()))) {//检查当前是否已经设置了密码
            // 如果已经设置，则先进入check模式
            layout_parent.switchToCheckMode(parsePwdStr(getPwd()), 5);//校验密码
        } else {//如果没有设置，则进入setting模式
            layout_parent.switchToResetMode();
            tv_go.setText(EasyGestureLockLayout.ToastStrHolder.forYourSafetyStr);
            tv_go.setTextColor(EasyGestureLockLayout.ColorHolder.COLOR_YELLOW);
        }

        tv_redraw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                layout_parent.initCurrentTimes();
                tv_redraw.setVisibility(View.INVISIBLE);
                layout_small.refreshPwdKeyboard(null);
                tv_go.setText("请重新绘制");
            }
        });
    }

    @Override
    protected void initViewData() {
    }


    private void showConfirmDialog() {
        View root = LayoutInflater.from(this).inflate(R.layout.dialog_gesture_err, null, false);
        View action_btn_pos = root.findViewById(R.id.action_btn_pos);
        final CommonDialog alertDialog = new CommonDialog(this, root, R.style.VipDialog);
        action_btn_pos.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onCheckFailed();
                alertDialog.dismiss();
            }
        });
        alertDialog.setCancelable(false);
        alertDialog.show();
    }

    /**
     * 文案的左右摇动动画;重写，因为效果和基类的有一个差别
     */
    @Override
    protected void animate(View tv_go) {
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(tv_go, "translationX", -20, 20, -20, 0);
        objectAnimator.setDuration(300);
        objectAnimator.setInterpolator(new LinearInterpolator());
        objectAnimator.addListener(new AnimatorListenerAdapter() {//重写之后，增加了这个lis
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                layout_small.refreshPwdKeyboard(null);
            }
        });
        objectAnimator.start();
    }

}
